/**
 *
 */
package cn.edu.cqupt.cyExam.controller;

import cn.edu.cqupt.cyExam.myBatis.bean.User;
import cn.edu.cqupt.cyExam.service.SettingService;
import cn.edu.cqupt.cyExam.utils.JSONResult;
import cn.edu.cqupt.cyExam.utils.Utils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


/**
 * @author Qxl
 *
 */
@RestController
@RequestMapping("Setting")
public class SettingController {

	@Resource
	private SettingService settingService;



	@PostMapping("UpdatePassWord")
	public JSONResult updatePassWord(@RequestParam("oldPassword")String oldPassword,
			@RequestParam("password")String password,@RequestParam("repassword")String repassword,HttpServletRequest request) {
		String usersAccountnumber=request.getParameter("usersAccountnumber");

		User user=(User)request.getServletContext().getAttribute(usersAccountnumber);
		Integer userid=user.getUser_id();
		/*Users user=(Users) session.getAttribute("_USER_");
		Integer userid=user.getUsersId();*/
		/*Users user =settingService.getUserByusersAccountnumber(usersAccountnumber);
		Integer userid=user.getUsersId();*/
		String OldPasswordByMD5=Utils.getMD5(oldPassword);
		String PasswordByMD5=Utils.getMD5(password);

		if(OldPasswordByMD5.equals(user.getUser_password())&&password.equals(repassword)&&!(oldPassword.equals(password))) {
			settingService.updatePassWord(userid,PasswordByMD5);

			return new JSONResult(0, "修改成功！",1, null);
		}

		return new JSONResult(1, "修改失败！",0, null);
	}

	@PostMapping("Info")
	public JSONResult info(User user) {
		settingService.info(user);
		return new JSONResult(0,"修改成功",1,null);
	}

	@GetMapping("getoldinfo")
	public JSONResult getoldinfo(String usersAccountnumber) {

		User users= settingService.getUserByusersAccountnumber(usersAccountnumber);

		return new JSONResult(0,"修改成功",1,users);

	}




	@GetMapping("Exit")
	public JSONResult exit(String usersAccountnumber,HttpServletRequest request) {
		request.getServletContext().removeAttribute(usersAccountnumber);

		return new JSONResult(0, "退出成功！",1, null);
	}
}

